package org.yan.stock.server.core;

import org.yan.stock.server.client.Client;
import org.yan.stock.server.client.Stock;
import org.yan.stock.server.util.MathUtil;
import org.yan.stock.server.util.StockUtil;

public class ParseCommand implements Runnable {
	
	private Client client;
	
	private double  threshold;
	
	public ParseCommand(Client client, double threshold) {
		this.client = client;
		this.threshold = threshold;
	}

	public void run() {
		Stock[] stocks = client.getStockArr();
		String[] data = StockUtil.getStocks(client.getStocks());
		for(int i = 0; i < stocks.length; i++) {
			parseValue(stocks[i], data[i]);
		}
		MessageNotifier.notify(client);
	}
	
	private void parseValue(Stock stock, String data) {
		if("".equals(data)) {
			return;
		}
		String[] cols = data.split(",");
		stock.setName(cols[0]);// this col is the chinese name of stock
		stock.setOffset(MathUtil.calcPerc(cols[2], cols[3], MathUtil.CALC_SCALE2));
		if(stock.getLatch() == 0) {
			if(Math.abs(stock.getOffset()) >= threshold) {
				stock.setHasMsg(true);
			}
		}
		else {
			if(Math.abs(stock.getOffset()) >= stock.getLatch()){
				stock.setHasMsg(true);
			}
		}
		stock.setDatetime(cols[31]);
	}

}
